package com.mjh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjh.mapper.WChxhXxpxjlDt1Mapper;
import com.mjh.pojo.WChxhXxpxjlDt1;
import com.mjh.service.IWChxhXxpxjlDt1Service;
import com.mjh.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * 学习培训经历 前端控制器
 *
 *
 * @author MJH
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/study")
public class WChxhXxpxjlDt1Controller {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IWChxhXxpxjlDt1Service iwChxhXxpxjlDt1Service;
    /**
     * 学习培训经历添加
     * @param dt1
     */
    @PostMapping("/studyadd")
    public void studyadd(@RequestBody WChxhXxpxjlDt1 dt1){
        Integer userId = (Integer) JwtUtil.getPayload(request, "userId");
        dt1.setCreateUserId(userId);
        iwChxhXxpxjlDt1Service.save(dt1);
    }

    /**
     * 学习培训经历批量或单一删除
     * @param studyid
     */
    @PostMapping("/studydle")
    public void studydle(@RequestBody Integer [] studyid){
        iwChxhXxpxjlDt1Service.removeByIds(Arrays.asList(studyid));
    }

    /**
     * 学习培训经理分页查询
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @return
     */
    @GetMapping("/studyslt")
    public ResponseEntity<Object> studyslt(@RequestParam(defaultValue = "1") Integer currentPage,
                                           @RequestParam(defaultValue = "10")  Integer pageSize){
        Page<WChxhXxpxjlDt1> page = new Page<>(currentPage,pageSize);
        Integer userId = (Integer)JwtUtil.getPayload(request, "userId");
        Page<WChxhXxpxjlDt1> wtzxPage = iwChxhXxpxjlDt1Service.studyslt(page,userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",wtzxPage.getTotal());
        map.put("currentPage",wtzxPage.getCurrent());
        map.put("list",wtzxPage.getRecords());
        return ResponseEntity.ok(map);
    }
}
